package cn.liutao.common.servlet;

import cn.hutool.cron.CronUtil;
import cn.liutao.auth.mapper.UserMapper;
import cn.liutao.cron.CronService;
import cn.liutao.dish.mapper.CuisineMapper;
import cn.liutao.dish.mapper.DiningTableMapper;
import cn.liutao.dish.mapper.DishMapper;
import cn.liutao.dish.mapper.TasteMapper;
import cn.liutao.order.mapper.*;
import com.mybatisflex.core.MybatisFlexBootstrap;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;


/**
 * @author liutao
 * @Description 初始化mybatisFlex
 * @date 2023/10/31 12:26
 * @Version 1.0
 */
@WebServlet(loadOnStartup = 1,name = "mybatisFlexServlet",value = "/mybatisFlexServlet")
@Slf4j
public class MybatisFlexServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config){
        log.info("实现MybatisFlex的初始化");
        //配置数据源
        HikariConfig hikariConfig = new HikariConfig();
        hikariConfig.setDriverClassName("com.mysql.cj.jdbc.Driver");
        hikariConfig.setUsername("orderdb");
        hikariConfig.setPassword("orderdb");
        hikariConfig.setJdbcUrl("jdbc:mysql://8.138.102.145:3306/orderdb");
        HikariDataSource dataSource = new HikariDataSource(hikariConfig);
        MybatisFlexBootstrap.getInstance()
                .setDataSource(dataSource)
                .addMapper(UserMapper.class).addMapper(CuisineMapper.class).addMapper(TasteMapper.class)
                .addMapper(DishMapper.class).addMapper(DiningTableMapper.class).addMapper(CartMapper.class)
                .addMapper(OrderMapper.class).addMapper(OrderCartRelationMapper.class).addMapper(OrderBillMapper.class)
                .addMapper(PayBillMapper.class)
                .start();
        CronService instance = CronService.getInstance();
        //启动定时任务
        instance.createCron();
    }


    @Override
    public void destroy() {
        super.destroy();
        CronUtil.stop();
    }
}
